package app.texas.com.devilfishhouse.View.Fragment.houuseList;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.universal_library.adapter.BaseViewPagerAdapter;
import com.universal_library.fragment.BaseFragment;

import app.texas.com.devilfishhouse.R;
import app.texas.com.devilfishhouse.weight.popbutton.PopupButton;
import app.texas.com.devilfishhouse.weight.popbutton.adapter.NormalAdapter;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by solin on 2018/7/16.
 */

public class HouseListFragmentView extends BaseFragment {
    @BindView(R.id.tab_nav)
    TabLayout mTabNav;

    @BindView(R.id.base_viewPager)
    ViewPager mBaseViewpager;

    @BindView(R.id.popbutton_houseType)
    PopupButton popbutton_houseType;

    @BindView(R.id.popbutton_housePrice)
    PopupButton pop_housePrice;

    @BindView(R.id.popbutton_houseArea)
    PopupButton pop_houseArea;

    @BindView(R.id.popbutton_more)
    PopupButton pop_more;

    @BindView(R.id.ll_parent)
    LinearLayout ll_parent;

    private String type;
    private BaseViewPagerAdapter mViewpageAdapter;
    final String[] arr = {"好评率", "订单数量"};
    private String[] housetypes = {"住宅", "商铺", "写字楼", "公寓"};
    private String[] housePrices = {"20000以下", "20000-25000", "25000-30000", "30000-35000", "35000-40000", "40000以上"};
    private String[] houseAreas = {"市南区", "市北区", "李沧区", "黄岛", "城阳", "即墨"};
    private String[] houseMore = {"价格", "靠近地铁"};

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_houselist;

    }

    @Override
    public void initBundle(Bundle mBundle) {
        super.initBundle(mBundle);
        if (!TextUtils.isEmpty(mBundle.getString("type"))) {
            type = mBundle.getString("type");
        }
    }

    @Override
    protected void initView(View view) {
        ButterKnife.bind(this, view);
        mTabNav = (TabLayout) mRoot.findViewById(R.id.tab_nav);
        mBaseViewpager = (ViewPager) mRoot.findViewById(R.id.base_viewPager);
        mViewpageAdapter = new BaseViewPagerAdapter(getChildFragmentManager(), getPagers(), getContext());
        mBaseViewpager.setAdapter(mViewpageAdapter);
        mTabNav.setupWithViewPager(mBaseViewpager);

        if (type.equals("1"))
            mBaseViewpager.setCurrentItem(1, true);
        else
            mBaseViewpager.setCurrentItem(0, true);
        initPopButton(popbutton_houseType, housetypes);
        initPopButton(pop_housePrice, housePrices);
        initPopButton(pop_houseArea, houseAreas);
        initPopButton(pop_more, houseMore);
    }

    @Override
    protected void initData() {

    }


    public BaseViewPagerAdapter.PagerInfo[] getPagers() {
        Bundle bundle = new Bundle();
        bundle.putString("type", "search");
        return new BaseViewPagerAdapter.PagerInfo[]{
                new BaseViewPagerAdapter.PagerInfo("新房", NewHouseFragment.class, bundle),
                new BaseViewPagerAdapter.PagerInfo("二手房", NewHouseFragment.class, bundle)

        };
    }

    private void initPopButton(final PopupButton pop, final String[] strings) {
        View popupview = LayoutInflater.from(getContext()).inflate(R.layout.popup, null);
        ListView lv = (ListView) popupview.findViewById(R.id.lv);

        final NormalAdapter adapter = new NormalAdapter(getContext(), R.layout.popup_item, strings, R.drawable.normal, R.drawable.press);
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                adapter.setPressPostion(position);
                adapter.notifyDataSetChanged();
                pop.setText(strings[position]);
                pop.hidePopup();
//                if (position == 0)
//                    getServiceProductList("", "", "satisfied_rank", "");
//                else
//                    getServiceProductList("", "", "order_count", "");
            }
        });
        pop.setPopupView(popupview, ll_parent);
    }
}
